package com.spark.modules.generation.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spark.common.core.Result;
import com.spark.common.query.QueryGenerator;
import com.spark.common.core.entity.generation.GenModel;
import com.spark.modules.generation.entity.GenModelClassify;
import com.spark.modules.generation.service.IGenModelClassifyService;
import com.spark.modules.generation.service.IGenModelService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 模型分类Controller
 *
 * @author LYCHEE
 * @date 2025/02/24 15:24
 */
@RestController
@RequestMapping(value = "/gen/modelClassify")
public class GenModelClassifyController {

    @Resource
    private IGenModelClassifyService genModelClassifyService;
    @Resource
    private IGenModelService genModelService;

    /**
     * 分页列表查询
     *
     * @param genModelClassify 模型分类对象
     * @param pageNo              分页
     * @param pageSize            分页
     * @return 模型分类列表
     */
    @GetMapping(value = "/queryPageList")
    public Result<?> queryPageList(GenModelClassify genModelClassify,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        QueryWrapper<GenModelClassify> queryWrapper = QueryGenerator.initQueryWrapper(genModelClassify);
        Page<GenModelClassify> page = new Page<GenModelClassify>(pageNo, pageSize);
        IPage<GenModelClassify> pageList = genModelClassifyService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 列表查询
     *
     * @param genModelClassify 模型分类对象
     * @return 模型分类列表
     */
    @GetMapping(value = "/queryList")
    public Result<?> queryList(GenModelClassify genModelClassify) {
        QueryWrapper<GenModelClassify> queryWrapper = QueryGenerator.initQueryWrapper(genModelClassify);
        List<GenModelClassify> list = genModelClassifyService.list(queryWrapper);
        return Result.ok(list);
    }

    /**
     * 通过ID查询
     *
     * @param id 主键
     * @return 模型分类对象
     */
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam String id) {
        GenModelClassify genModelClassify = genModelClassifyService.getById(id);
        if (Objects.isNull(genModelClassify)) {
            return Result.fail("数据获取失败");
        }
        return Result.ok(genModelClassify);
    }

    /**
     * 新增
     *
     * @param genModelClassify 模型分类对象
     * @return 成功或失败
     */
    @PostMapping(value = "/add")
    public Result<?> add(@Validated @RequestBody GenModelClassify genModelClassify) {
        boolean save = genModelClassifyService.save(genModelClassify);
        return Result.flag(save);
    }

    /**
     * 编辑
     *
     * @param genModelClassify 模型分类对象
     * @return 成功或失败
     */
    @PostMapping(value = "/edit")
    public Result<?> edit(@Validated @RequestBody GenModelClassify genModelClassify) {
        boolean update = genModelClassifyService.updateById(genModelClassify);
        return Result.flag(update);
    }

    /**
     * 通过id删除
     *
     * @param id 主键
     * @return 成功或失败
     */
    @DeleteMapping(value = "/delete/{id}")
    public Result<?> delete(@PathVariable(name = "id") String id) {
        LambdaQueryWrapper<GenModel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(GenModel::getModelClassifyId, id);
        long count = genModelService.count(lambdaQueryWrapper);
        if (count > 0){
            return Result.fail("该分类下存在模型!");
        }
        boolean update = genModelClassifyService.removeById(id);
        return Result.flag(update);
    }

    /**
     * 通过id批量删除删除
     *
     * @param ids 主键
     * @return 成功或失败
     */
    @DeleteMapping(value = "/deleteBatchByIds/{ids}")
    public Result<?> deleteBatchByIds(@PathVariable(name = "ids") String[] ids) {
        boolean update = genModelClassifyService.removeBatchByIds(Arrays.asList(ids));
        return Result.flag(update);
    }

    /**
     * 获取模型分类和模型
     *
     * @param searchContent 模型名称或编码
     * @return 分类和模型数据
     */
    @GetMapping(value = "/queryClassifyAndModel")
    public Result<?> queryClassifyAndModel(String searchContent) {
        return genModelClassifyService.queryClassifyAndModel(searchContent);
    }

}
